package com.zhou.juc.test07_Unsafe;

import sun.misc.Unsafe;

/**
 * @author zhouyanxiang
 * @create 2020-08-2020/8/11-22:18
 */
public class HelloUnsafe {

    static class M {
        private M() {}

        int i =0;
    }

    public static void main(String[] args) throws InstantiationException {
        Unsafe unsafe = Unsafe.getUnsafe();
        M m = (M)unsafe.allocateInstance(M.class);
        m.i = 9;
        System.out.println(m.i);
//        try {
//            Field field = Unsafe.class.getDeclaredField("theUnsafe");
//            // 将字段的访问权限设置为true
//            field.setAccessible(true);
//            // 因为theUnsafe字段在Unsafe类中是一个静态字段，所以通过Field.get()获取字段值时，可以传null获取
//            Unsafe unsafe = (Unsafe) field.get(M.class);
//            // 控制台能打印出对象哈希码
//            System.out.println(unsafe);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }
}
